<?php
/**
 * Copyright (c) 2013 pipapai.com
 */
JLoader::register('PJobpositionModelPosition', OC::$SERVERROOT.'/apps/jobposition/lib/position.php');
JLoader::register('PJobpositionModelPositions', OC::$SERVERROOT.'/apps/jobposition/lib/positions.php');
JLoader::register("PCompanyModelCompany", OC::$SERVERROOT."/apps/company/lib/company.php"); 
JLoader::register("PJobpositionHelper", OC::$SERVERROOT.'/apps/jobposition/lib/helper.php');
JLoader::register('ProvinceHelper', JPATH_SITE.'/components/com_city/helper.php');
JLoader::register("PCompanyHelper", OC::$SERVERROOT."/apps/company/lib/helper.php");
JLoader::register("PJobpositionHelper", OC::$SERVERROOT.'/apps/jobposition/lib/helper.php');
JLoader::register("PCompanyModelSubAccounts", OC::$SERVERROOT.'/apps/company/lib/subaccounts.php');
JLoader::register("UserPrivacy", JPATH_SITE.'/components/com_puser/userprivacy.php');

class PJobpositionController {
   
   public static function folder(){
      $app=PFactory::getApplication();
      $model = new PJobpositionModelPositions();

      $jobdir = JRequest::getString('jobdir', null);
      $jobpositiondir['jobsdir']=$jobdir;
      $foldername = JRequest::getString('foldername',null);
      if($jobdir == null && $foldername !== null) {
         // foldername is given, get integer jobdir
         $jobfolder = $model->getJobsFolderIdByName($foldername);
         if($jobfolder) {
            $jobdir = $jobfolder->folder_id; 
            $foldername = $jobfolder->summary; 
            // if foldername is true, it's auto-created subfolder, do redirect so we can customize client folder name 
            if($foldername) {
               OC_Response::redirect(OC_Helper::linkToRoute('jobpos_folder')."?jobdir=".$jobdir."&foldername=".JFilterOutput::stringURLUnicodeSlug($foldername));
               return; 
            }
         }
         
      }
      if(!$jobdir) $jobdir = 'default';

      $keyword = JRequest::getString('keyword', null);
      $jobpositiondir=$app->getUserState('jobpositiondir',null);     
      if($jobpositiondir){
         unset($jobpositiondir['jobsmatch']);
         unset($jobpositiondir['jobssyns']);
         unset($jobpositiondir['jobsarea']);
         unset($jobpositiondir['jobssource']);
         unset($jobpositiondir['jobsdir']);
         unset($jobpositiondir['jobskeyword']);
         unset($jobpositiondir['jobssearch']);
         unset($jobpositiondir['switchsearch']);
      }
      $jobpositiondir['jobskeyword']=$keyword;
      $jobpositiondir['jobsdir']=$jobdir;
      $app->setUserState('jobpositiondir', $jobpositiondir);
      self::home();
   }
   
   public static function home() {
      if(!PUtil::checkUserLogin(true))
         return;      
      if(PFactory::isJobseekerUser()) {
         OC_Response::redirect(OC_Helper::linkToRoute("jobseeker_home"));
         return;
      }
      $app=PFactory::getApplication();
      $jobpositiondir=$app->getUserState('jobpositiondir',null);
      $syncselected=array();
      $areaselected=array();
      $sourceselected=array();
      $sync=array('0'=>'未同步','1'=>'同步失败','2'=>'同步成功');
      $source=array('pipapai'=>'枇杷派','51job'=>'前程无忧','zhilian'=>'智联招聘','lagou'=>'拉勾','jiancai'=>'建筑英才');
      $areacheckboxlist="";
      $synccheckboxlist="";
      $sourcecheckboxlist="";
      $sync=PJobpositionHelper::getSelectOptionArray($sync);
      $source=PJobpositionHelper::getSelectOptionArray($source);
      if(isset($jobpositiondir['jobssyns'])){
         $syncselected=explode(',', $jobpositiondir['jobssyns']);
      }
      $synccheckboxlist=PJobpositionHelper::checkboxList($sync, 'sync[]', '', 'value', 'text',$syncselected,'sync_all','selectall');
      if(isset($jobpositiondir['jobssource'])){
         $sourceselected=explode(',', $jobpositiondir['jobssource']);
      }
      $sourcecheckboxlist=PJobpositionHelper::checkboxList($source, 'source[]','','value','text',$sourceselected,'source_all','selectall');
      
      $user = PFactory::getMasterUser();
      $model = new PJobpositionModelPositions();

      if(PCompanyHelper::isJobManager() && !PCompanyHelper::isJobManagerViewOnly()) {
         $permissions = OCP\PERMISSION_READ;
         $permissions |= OCP\PERMISSION_CREATE;
         $permissions |= OCP\PERMISSION_UPDATE;
         $permissions |= OCP\PERMISSION_DELETE;
      }
      else {
         // default to read permission
         $permissions = OCP\PERMISSION_READ;
      }
      
      if(JArrayHelper::getValue($jobpositiondir, 'jobsdir', 'all') == 'default') {
         list($joblist, $pagination) = $model->getList($user->id, 'saved');
         if (empty($joblist)) {
            list($joblist, $pagination) = $model->getList($user->id, 'all');
            $jobpositiondir['jobsdir'] = 'all';
         } else {
            $jobpositiondir['jobsdir'] = 'saved';
         }
         $jobpositiondir = $app->setUserState('jobpositiondir', $jobpositiondir);     
      } else {
         list($joblist, $pagination) = $model->getList($user->id);
      }
      
      list($starttime, $endtime) = PResumesHelper::getTimeRange();
      $todaystart = PFactory::getDate($starttime)->format('Y-m-d H:i:s');

      $alljoblist=$model->getCurrentCompanyJobList();
      if($alljoblist){
         $areaarray=PJobpositionHelper::getSelectOptionArray($alljoblist);
         if(isset($jobpositiondir['jobsarea'])){
            $areaselected=explode(',', $jobpositiondir['jobsarea']);
         }
         $areacheckboxlist=PJobpositionHelper::checkboxList($areaarray, 'area[]', '', 'value', 'text',$areaselected,'area_all','selectall');
      }
   
      $part_position_toptab = new OCP\Template('jobposition', 'part.top', '');
      $part_position_toptab->assign('jobsdir', strtolower(JArrayHelper::getValue($jobpositiondir, 'jobsdir', 'all')));
      
      $part_position_modal = new OCP\Template('jobposition', 'part.modal', '');

      $tmpl = new OCP\Template('jobposition', 'home', 'user');
      $tmpl->assign('part_position_toptab', $part_position_toptab->fetchPage());
      $tmpl->assign('part_position_modal', $part_position_modal->fetchPage());
      $tmpl->assign('joblist', $joblist);
      $tmpl->assign('starttime', $starttime);
      $tmpl->assign('todaystart', $todaystart);
      $tmpl->assign('pagination', $pagination);
      $tmpl->assign('page_title', '职位管理');
      $tmpl->assign('permissions', $permissions);
      $tmpl->assign('orderBy', JArrayHelper::getValue($jobpositiondir, 'orderby', 'publish_date'));
      $tmpl->assign('orderDir', strtolower(JArrayHelper::getValue($jobpositiondir, 'orderdir', 'desc')));
      $tmpl->assign('jobsmatch', JArrayHelper::getValue($jobpositiondir, 'jobsmatch', 'all'));
      $tmpl->assign('jobsdir', strtolower(JArrayHelper::getValue($jobpositiondir, 'jobsdir', 'all')));
      $tmpl->assign('donejobsearch', strtolower(JArrayHelper::getValue($jobpositiondir, 'jobssearch', 'undone')));
      $tmpl->assign('syncheckboxlist', $synccheckboxlist);
      $tmpl->assign('areacheckboxlist', $areacheckboxlist);
      $tmpl->assign('sourcecheckboxlist', $sourcecheckboxlist);
      $tmpl->assign('masteruser', PFactory::isMasterUser());
      $tmpl->printPage();
   }
   
   //search
   public static function searchJobPosition(){
      if(!PUtil::checkUserLogin(true))
         return;
      if(PFactory::isJobseekerUser()) {
         echo '您无权访问该资源';
         return false;
      }
      $app=PFactory::getApplication();
      $jobpositiondir=$app->getUserState('jobpositiondir',null);
      if($jobpositiondir){
         unset($jobpositiondir['jobsmatch']);
         unset($jobpositiondir['jobssyns']);
         unset($jobpositiondir['jobsarea']);
         unset($jobpositiondir['jobssource']);
      }
      
      $jobdir = JRequest::getString('jobdir', 'all');
      $keyword = JRequest::getString('keyword', null);
      $jobpositiondir['jobsdir']=$jobdir;
      $jobpositiondir['jobskeyword']=$keyword;
      
      $jobsmatch=JRequest::getString('jobsmatch','');    
      $jobssync = JRequest::getVar("sync", array(), 'GET', 'array');
      //jobs_match
      $jobpositiondir['jobsmatch']=$jobsmatch;
      //jobs_sync
      if($jobssync){
         $jobssync=implode(',', $jobssync);
         $jobpositiondir['jobssyns']=$jobssync;       
      }
      //jobs_area
      $jobsarea=JRequest::getVar("area", array(), 'GET', 'array');
      if($jobsarea){
         $jobsarea=implode(',', $jobsarea);
         $jobpositiondir['jobsarea']=$jobsarea;
      } 
      //jobs_source
      $jobssource=JRequest::getVar("source", array(), 'GET', 'array');
      if($jobssource){
         $jobssource=implode(',', $jobssource);
         $jobpositiondir['jobssource']=$jobssource;
      }
      $jobpositiondir['jobssearch']='done';
      $app->setUserState('jobpositiondir', $jobpositiondir);
     
      self::home();     
   }
    
   public static function jobdemandssave() {
      OC_JSON::setContentTypeHeader();
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
         return;
      }
      if(PFactory::isJobseekerUser()) {
         OCP\JSON::error(array('error' => '您无权访问该资源'));
         return false;
      }

      $jobid = JRequest::getInt('job_id', 0);

      if(!PCompanyHelper::checkJobManagerPermissions($jobid)) {
         jexit(jsonError('您无权访问该资源', null, false));
      }

      $model = new PJobpositionModelPosition();
      if(!$model->saveJobDemands()) {
         jexit(jsonError($model->getError(), null, false));
      }

      $user = PFactory::getMasterUser();

      // output the latest demands html 
      $jobdemands = $model->showInterviews($user->id, $jobid, false);
      $demands_html = new OCP\Template('jobposition', 'part.interview.html', '');
      $demands_html->assign('jobdemands', $jobdemands); 
      jexit($demands_html->fetchPage());
   }
   
   public static function getmatchrule(){
      if(PFactory::isJobseekerUser()) {
         echo '您无权访问该资源';
         return false;
      }
      
      $user = PFactory::getMasterUser();
      $jobid = JRequest::getInt('id',0);
      $model = new PJobpositionModelPosition();
      
      if(!PCompanyHelper::checkJobManagerPermissions($jobid)) {
         echo '您无权访问该资源';
         return false;
      }
      
      $jobdemands = $model->showInterviews($user->id, $jobid);
      $demands_html = new OCP\Template('jobposition', 'part.interview.html', '');
      $demands_html->assign('jobdemands', $jobdemands); 
      $demands_html->printPage();
   }

   public static function getonlinetest(){
      if(PFactory::isJobseekerUser()) {
         echo '您无权访问该资源';
         return false;
      }
      
      $user = PFactory::getMasterUser();
      $jobid = JRequest::getInt('id',0);
      $model = new PJobpositionModelPosition();
      
      if(!PCompanyHelper::checkJobManagerPermissions($jobid)) {
         echo '您无权访问该资源';
         return false;
      }
      
      $jobsheet_details = $model->showJobsheetDetails($jobid);
      $jobdetails = $model->showJobdetails($jobid);
      $showsheets = $model->showSheets();
      
      $tmpl = new OCP\Template('jobposition', 'part.onlinetest', '');
      $tmpl->assign('jobdetails', $jobdetails); 
      $tmpl->assign('jobsheet_details', $jobsheet_details); 
      $tmpl->assign('showsheets', $showsheets); 
      $tmpl->printPage();
   }

   public static function getjobdemands(){
      if(PFactory::isJobseekerUser()) {
         echo '您无权访问该资源';
         return false;
      }
      
      $user = PFactory::getMasterUser();
      $jobid = JRequest::getInt('id',0);
      $model = new PJobpositionModelPosition();
      
      if(!PCompanyHelper::checkJobManagerPermissions($jobid)) {
         echo '您无权访问该资源';
         return false;
      }
      
      $jobdetails = $model->showJobdetails($jobid);
      $jobdemands = $model->showInterviews($user->id, $jobid);
      
      $demands_html = new OCP\Template('jobposition', 'part.interview.html', '');
      $demands_html->assign('jobdemands', $jobdemands); 
      
      $tmpl = new OCP\Template('jobposition', 'part.interview', '');
      $tmpl->assign('demands_html', $demands_html->fetchPage()); 
      $tmpl->assign('jobdemands', $jobdemands); 
      $tmpl->assign('jobdetails', $jobdetails); 
      $tmpl->printPage();
   }

   public static function getpersontest(){
      if(PFactory::isJobseekerUser()) {
         echo '您无权访问该资源';
         return false;
      }
      
      $user = PFactory::getMasterUser();
      $jobid = JRequest::getInt('id',0);

      $model = new PJobpositionModelPosition();
      
      if(!PCompanyHelper::checkJobManagerPermissions($jobid)) {
         echo '您无权访问该资源';
         return false;
      }
      
      $persontestinfo = $model->showPersontests($user->id, $jobid);
      $persontest_details = $model->showPersontestsheetDetails($jobid);
      $jobdetails = $model->showJobdetails($jobid);
      
      $tmpl = new OCP\Template('jobposition', 'part.persontest', '');
      $tmpl->assign('persontestinfo', $persontestinfo); 
      $tmpl->assign('jobdetails', $jobdetails); 
      $tmpl->assign('persontest_details', $persontest_details); 
      $tmpl->printPage();
   }

   public static function savepersontest() {
      OC_JSON::setContentTypeHeader();
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
         return;
      }
      if(PFactory::isJobseekerUser()) {
         OCP\JSON::error(array('error' => '您无权访问该资源'));
         return false;
      }

      $job_id = JRequest::getInt('job_id',0);

      if(!PCompanyHelper::checkJobManagerPermissions($job_id)) {
         jexit(jsonError('您无权访问该资源', null, false));
      }
 
      $model = new PJobpositionModelPosition();
      if(!$model->savepersontest()){
         jexit(jsonError($model->getError(), null, false));
      }
      jexit(jsonSuccess('更新成功', null, null, false));
   }

   public static function editSheet() {
      if(!PUtil::checkUserLogin(true))
         return;

      if(PFactory::isJobseekerUser()) {
         echo "您无权访问该资源";
         return false;
      }

      $sheetid = JRequest::getInt('id',0);
      $jobid = JRequest::getInt('jobid',0);

      $model = new PJobpositionModelPosition();
      
      if(!PCompanyHelper::checkJobManagerPermissions($jobid)) {
         echo '您无权访问该资源';
         return false;
      }
      
      $jobsheet_detail = $model->showJobsheetDetails($jobid);
      $job_detail = $model->showJobdetails($jobid);
      $sheet_detail = $model->getSheetDetails($sheetid);
      $questions = $model->initQuestions($sheetid);

      $jobpos_head = new OCP\Template('jobposition', 'part.head', '');
      $jobpos_head->assign('jobdetails', $job_detail);
      $jobpos_head->assign('current', 'onlinetest');

      $tmpl = new OCP\Template('jobposition', 'sheet.edit', 'user');
      $tmpl->assign('jobpos_head', $jobpos_head->fetchPage());
      $tmpl->assign('sheet_detail', $sheet_detail);
      $tmpl->assign('jobsheet_detail', $jobsheet_detail);
      $tmpl->assign('questions', $questions);
      $tmpl->assign('job_detail', $job_detail);
      $tmpl->assign('page_title', '线上笔试');
      $tmpl->printPage();
   }

   public static function saveSheet() {
      OC_JSON::setContentTypeHeader();
      if(!OC_User::isLoggedIn()) {
         jexit(jsonSuccess('success', OC_Helper::linkToRoute("company_login"), null, false));
      }
      if(PFactory::isJobseekerUser()) {
         jexit(jsonError('您无权访问该资源', null, false));
      }

      $job_id = JRequest::getInt('job_id',0);

      if(!PCompanyHelper::checkJobManagerPermissions($job_id)) {
         jexit(jsonError('您无权访问该资源', null, false));
         return false;
      }

      $model = new PJobpositionModelPosition();
      $result = $model->saveSheet();

      if (!$result) {
         jexit(jsonError($model->getError(), null, false));
      } else {
         if (is_array($result)) {
            $redirect = OC_Helper::linkToRoute("jobpos_editsheet",$result);
         } else {
            $redirect = null;
         }
         jexit(jsonSuccess('success', $redirect, null, false));
      }
   }

   public static function saveQuestion() {
      OC_JSON::setContentTypeHeader();
      if(!OC_User::isLoggedIn()) {
         jexit(jsonSuccess('success', OC_Helper::linkToRoute("company_login"), null, false));
      }
      if(PFactory::isJobseekerUser()) {
         jexit(jsonError('您无权访问该资源', null, false));
      }

      $model = new PJobpositionModelPosition();

      $data = $model->saveQuestion();
      if (!$data) {
         jexit(jsonError($model->getError(), null, false));
      } else {
         jexit(jsonSuccess('保存成功', null, $data, false));
      }
   }

   public static function deleteQuestion() {
      OC_JSON::setContentTypeHeader();
      if(!OC_User::isLoggedIn()) {
         jexit(jsonSuccess('success', OC_Helper::linkToRoute("company_login"), null, false));
      }
      if(PFactory::isJobseekerUser()) {
         jexit(jsonError('您无权访问该资源', null, false));
      }

      $model = new PJobpositionModelPosition();

      if (!$model->deleteQuestion()) {
         jexit(jsonError($model->getError(), null, false));
      } else {
         jexit(jsonSuccess('success', null, null, false));
      }
   }

   public static function deleteSheet() {
      OC_JSON::setContentTypeHeader();
      if(!OC_User::isLoggedIn()) {
         jexit(jsonSuccess('success', OC_Helper::linkToRoute("company_login"), null, false));
      }
      if(PFactory::isJobseekerUser()) {
         jexit(jsonError('您无权访问该资源', null, false));
      }

      $model = new PJobpositionModelPosition();

      $jobid = JRequest::getInt('jobid', 0);

      if (!$model->deleteSheet()) {
         jexit(jsonError($model->getError(), null, false));
      } else {
         jexit(jsonSuccess('success', OC_Helper::linkToRoute("jobpos_setup", array('id'=>$jobid)).'#onlinetest', null, false));
      }
   }

   public static function previewsheet() {
      if(!PUtil::checkUserLogin(true))
         return;

      if(PFactory::isJobseekerUser()) {
         echo '您无权访问该资源';
         return false;
      }

      $sheetid = JRequest::getInt('id',0);
      $jobid = JRequest::getInt('jobid',0);

      $job_title = JRequest::getString('job_title','');
      $user = PFactory::getMasterUser();

      $model = new PJobpositionModelPosition();
      
      if(!PCompanyHelper::checkJobManagerPermissions($jobid)) {
         echo '您无权访问该资源';
         return false;
      }
      
      $sheetdetails = $model->getSheetDetails($sheetid);
      $exampaper = $model->getExamQuestions($sheetid);
      $jobdetails = $model->showJobdetails($jobid);
      
      $tmpl = new OCP\Template('jobposition', 'previewsheet', 'user');
      $tmpl->assign('sheetdetails', $sheetdetails); 
      $tmpl->assign('exampaper', $exampaper); 
      $tmpl->assign('jobdetails', $jobdetails); 
      $tmpl->assign('user', $user); 
      $tmpl->assign('page_title', '考卷预览');
      $tmpl->printPage();
   }

   // load more exam questions
   public static function loadmore() {
      if(PFactory::isJobseekerUser()) {
         echo '您无权访问该资源';
         return false;
      }
       
      if(!PFactory::isMasterUser()) {
         echo '您无权访问该资源';
         return false;
      }
             
      $app = PFactory::getApplication();
      $get = JRequest::get("get");

      $start = $get['last_end'];
      $total = $get['total_num'];
      $sheetid = $get['sheetid'];
      if ($start > $total) {
         return;
      }

      $model = new PJobpositionModelPosition();
      $sheetdetails = $model->getSheetDetails($sheetid);
      $exampaper = $model->getExamQuestions($sheetid, $start, 5);

      $tmpl = new OCP\Template('jobposition', 'loadmore','');
      $tmpl->assign('sheetdetails', $sheetdetails);
      $tmpl->assign('exampaper', $exampaper);
      $tmpl->assign('start', $start);
      $tmpl->printPage();
   }

   public static function choosejobsheet() {
      OC_JSON::setContentTypeHeader();
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
         return;
      }
      if(PFactory::isJobseekerUser()) {
         OCP\JSON::error(array('error' => '您无权访问该资源'));
         return false;
      }

      $jobid = JRequest::getInt('jobid',0);
      $sheetid = JRequest::getInt('sheetid',0);
      
      if(!PCompanyHelper::checkJobManagerPermissions($jobid)) {
         jexit(jsonError('您无权访问该资源', null, false));
      }

      $model = new PJobpositionModelPosition();
      if(!$model->chooseJobsheet($jobid, $sheetid)){
         jexit(jsonError($model->getError(), null, false));
      }
      
      jexit(jsonSuccess('更新成功', null, null, false));
   }
   
   public static function canceljobsheet() {
      OC_JSON::setContentTypeHeader();
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
         return;
      }
      if(PFactory::isJobseekerUser()) {
         OCP\JSON::error(array('error' => '您无权访问该资源'));
         return false;
      }

      $jobid = JRequest::getInt('jobid',0);
      
      if(!PCompanyHelper::checkJobManagerPermissions($jobid)) {
         jexit(jsonError('您无权访问该资源', null, false));
      }

      $model = new PJobpositionModelPosition();
      if(!$model->cancelJobsheet($jobid)){
         jexit(jsonError($model->getError(), null, false));
      }
      
      jexit(jsonSuccess('更新成功', null, null, false));
   }
   
   public static function uploadsheet() {
      OC_JSON::setContentTypeHeader('text/plain');
      if(!OC_User::isLoggedIn()) {
         OC_Response::redirect(OC_Helper::linkToRoute("company_login"));
         return;
      }
      if(PFactory::isJobseekerUser()) {
         OCP\JSON::error(array('error' => '您无权访问该资源'));
         return false;
      }
            
      $jobid = JRequest::getInt('jobid',0);
      
      if(!PCompanyHelper::checkJobManagerPermissions($jobid)) {
         OCP\JSON::error(array('error' => '您无权访问该资源'));
         return false;
      }

      $model = new PJobpositionModelPosition();
      jexit($model->uploadSheet($jobid));
   }
   
   public static function setup() {
      if(!PUtil::checkUserLogin(true))
         return;

      if(PFactory::isJobseekerUser()) {
         echo '您无权访问该资源';
         return false;
      }

      $user = PFactory::getMasterUser();
      $jobid = JRequest::getInt('id',0);
      
      if(!PCompanyHelper::ownJobId($jobid)) {
         echo '您无权访问该资源';
         return false;
      }
      $app=PFactory::getApplication();
      $jobpositiondir=$app->getUserState('jobpositiondir',null);
      $jobsdir=strtolower(JArrayHelper::getValue($jobpositiondir, 'jobsdir', 'all'));

      $session = PFactory::getSession(); 
      $syncto = $session->get('jobpos_syncto');
      $addjob = $session->get('jobpos_addjob', 0);
      // only pop up first time 
      $session->clear('jobpos_syncto');
      $session->clear('jobpos_addjob');

      $model = new PJobpositionModelPosition();
      $jobdetails = $model->showJobdetails($jobid);
      if($jobdetails->system){
         echo '该职位是系统上传职位，不允许修改匹配设置。';
         return false;
      }
      
      $adminuser = PCompanyHelper::checkJobManagerPermissions($jobid);
      $edit = ($adminuser && !$jobdetails->is_trashed) ? true : false;
      $jobsdir = (!$jobdetails->is_trashed && $jobsdir=='trashed') ? 'all' : $jobsdir;
      
      $jobpos_head = new OCP\Template('jobposition', 'part.head', '');
      $jobpos_head->assign('jobdetails', $jobdetails); 
      $part_jobdetails = new OCP\Template('jobposition', 'part.jobdetails', '');
      $part_jobdetails->assign('adminuser', $adminuser);
      $part_jobdetails->assign('edit', $edit);
      $part_jobdetails->assign('jobdetails', $jobdetails);
      $part_jobdetails->assign('syncto', empty($syncto) ? '' : implode(',', $syncto));
      $part_jobdetails->assign('addjob', $addjob);

      $part_position_toptab = new OCP\Template('jobposition', 'part.top', '');
      $part_position_toptab->assign('jobsdir', $jobsdir);
      
      $part_position_modal = new OCP\Template('jobposition', 'part.modal', '');
      
      $tmpl = new OCP\Template('jobposition', 'part.base', 'user');
      $tmpl->assign('part_position_toptab', $part_position_toptab->fetchPage());
      $tmpl->assign('part_position_modal', $part_position_modal->fetchPage());
      $tmpl->assign('jobpos_head', $jobpos_head->fetchPage()); 
      $tmpl->assign('part_jobdetails', $part_jobdetails->fetchPage());
      $tmpl->assign('adminuser', $adminuser);
      $tmpl->assign('edit', $edit);
      $tmpl->assign('jobdetails', $jobdetails); 
      $tmpl->assign('page_title', '职位设置');
      $tmpl->printPage();
   }
   
   public static function postjob() {
      if(!PUtil::checkUserLogin(true))
         return;
   
      if(!PCompanyHelper::checkCRUDPermission()) {
         echo '您无权访问该资源';
         return ;
      }
      $app=PFactory::getApplication();
      $jobpositiondir=$app->getUserState('jobpositiondir',null);
   
      $user = PFactory::getMasterUser();
      $part_postjob = new OCP\Template('jobposition', 'part.postjob', '');
      $part_postjob->assign('type','add');
      $part_postjob->assign('jobdetails', '');
      
      $part_position_toptab = new OCP\Template('jobposition', 'part.top', '');
      $part_position_toptab->assign('jobsdir', strtolower(JArrayHelper::getValue($jobpositiondir, 'jobsdir', 'all')));
      
      $tmpl = new OCP\Template('jobposition', 'postjob', 'user');
      $tmpl->assign('part_position_toptab', $part_position_toptab->fetchPage());
      $tmpl->assign('page_title', '发布职位');
      $tmpl->assign('jobpos_postjob', $part_postjob->fetchPage());
      $tmpl->printPage();
   }
   
   public static function saveJobPosition(){
      OC_JSON::setContentTypeHeader();
      if(!OC_User::isLoggedIn()) {
         jexit(jsonError('请先登录', null, false));
      }
        
      $model = new PJobpositionModelPosition();
      if(!$model->saveJobPosition()) {
         jexit(jsonError($model->getError(), null, false));
      }
      $app = PFactory::getApplication();
      $jobpositiondir['orderby'] = 'publish_date';
      $jobpositiondir['orderdir'] = 'DESC';
      $app->setUserState('jobpositiondir', $jobpositiondir);

      jexit(jsonSuccess('success', OC_Helper::linkToRoute("jobpos_setup", array('id'=>$model->get('jobid'))), null, false));
    }
    
   public static function view() {
      $job_id=JRequest::getInt('id',0);
      $user="";
      $error_msg = "";
      $model = new PJobpositionModelPosition();
      $jobdetail = $model->showJobdetails($job_id);
      if(empty($jobdetail) || $jobdetail->system){
         $error_msg = '该职位可能不存在或不合法，请重新确认。';
      }
      if(!empty($jobdetail->employer_id)){
         $user = JobHelper::getEmployerProfileDetails($jobdetail->employer_id, true);
      }      
      $tmpl = new OCP\Template('jobposition', 'view', 'base');
      $tmpl->assign('page_title', '职位预览');
      $tmpl->assign('user', $user);
      $tmpl->assign('jobdetail', $jobdetail);
      $tmpl->assign('error_msg', $error_msg);
      $tmpl->printPage();
   }


   public static function renewjob(){
      OC_JSON::setContentTypeHeader();
      if(!OC_User::isLoggedIn()) {
         jexit(jsonError('请先登录', null, false));
      }
      if(PFactory::isJobseekerUser()) {
         jexit(jsonError('您无权访问该资源', null, false));
      }

      $jobids = JRequest::getString('jobids','');
      $jobidsarr = array();
      if(!empty($jobids)){
         $jobidsarr = explode(',', $jobids);
      }
      JArrayHelper::toInteger($jobidsarr);

      if(empty($jobidsarr)){
         jexit(jsonError('您无权访问该资源', null, false));
      }
      $model = new PJobpositionModelPosition();
      $jobpermissionids = $model->checkPermissions($jobidsarr);
      if(empty($jobpermissionids)) {
         jexit(jsonError('您无权访问该资源', null, false));
      }

      $result = $model->renewJob();
      if(!$result) {
         jexit(jsonError($model->getError(), null, false));
      }

      $app = PFactory::getApplication();
      jexit(jsonSuccess(array('stat'=>'success','data'=>$result), null, null, false));
   }
}
